package com.sicpa.system.service.impl;

import com.sicpa.common.core.domain.AjaxResult;
import com.sicpa.common.core.domain.entity.AntiCounterfeitingRegisteredOffice;
import com.sicpa.common.core.domain.entity.AntiCounterfeitingUpdateContract;
import com.sicpa.common.core.domain.entity.SysUser;
import com.sicpa.common.core.domain.model.LoginUser;
import com.sicpa.common.enums.ExpertFileNameEnum;
import com.sicpa.common.enums.ExpertFilePathEnum;
import com.sicpa.common.enums.ProcessOptItemEnum;
import com.sicpa.common.enums.RoleEnum;
import com.sicpa.common.utils.ExcelTemplateExportUtils;
import com.sicpa.common.utils.SecurityUtils;
import com.sicpa.system.mapper.AntiCounterfeitingRegisteredOfficeMapper;
import com.sicpa.system.mapper.AntiCounterfeitingUpdateContractMapper;
import com.sicpa.system.mapper.SysUserMapper;
import com.sicpa.system.service.IAntiCounterfeitingUpdateContractService;
import com.sicpa.system.service.ICheckUserPowerService;
import com.sicpa.system.service.ISysNoticeService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * AntiCounterfeitingUpdateContractServiceImpl
 *
 * @author lwf
 */
@Service
public class AntiCounterfeitingUpdateContractServiceImpl implements IAntiCounterfeitingUpdateContractService {

    private AntiCounterfeitingUpdateContractMapper antiCounterfeitingUpdateContractMapper;

    private AntiCounterfeitingRegisteredOfficeMapper antiCounterfeitingRegisteredOfficeMapper;

    private SysUserMapper sysUserMapper;

    private ICheckUserPowerService iCheckUserPowerService;

    private ISysNoticeService iSysNoticeService;

    @Autowired
    public AntiCounterfeitingUpdateContractServiceImpl(AntiCounterfeitingUpdateContractMapper antiCounterfeitingUpdateContractMapper, AntiCounterfeitingRegisteredOfficeMapper antiCounterfeitingRegisteredOfficeMapper,SysUserMapper sysUserMapper,ICheckUserPowerService iCheckUserPowerService,ISysNoticeService iSysNoticeService) {
        this.antiCounterfeitingUpdateContractMapper = antiCounterfeitingUpdateContractMapper;
        this.antiCounterfeitingRegisteredOfficeMapper = antiCounterfeitingRegisteredOfficeMapper;
        this.sysUserMapper = sysUserMapper;
        this.iCheckUserPowerService = iCheckUserPowerService;
        this.iSysNoticeService = iSysNoticeService;
    }

    /**
     * 事务所申请修改联系人
     *
     * @param antiCounterfeitingUpdateContract
     * @return
     */
    @Override
    public AjaxResult applyUpdateContacts(AntiCounterfeitingUpdateContract antiCounterfeitingUpdateContract) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUser().getUserId();
        AntiCounterfeitingUpdateContract query = new AntiCounterfeitingUpdateContract();
        query.setApplicantId(userId);
        query.setStatus(1);
        List<Map<String, Object>> list = antiCounterfeitingUpdateContractMapper.selectList(query);
        if (list.size()>0 ) {
            return AjaxResult.error("修改事务所操作人'" + antiCounterfeitingUpdateContract.getContacts() + "'失败，操作人已申请过修改操作人，请等待审核");
        }
        SysUser sysUser = new SysUser();
        sysUser.setPhonenumber(antiCounterfeitingUpdateContract.getContactMobile());
        List<SysUser> userlist = sysUserMapper.selectUserList(sysUser);
        if (userlist.size()>0 && !userId.equals(userlist.get(0).getUserId())) {
            return AjaxResult.error("修改事务所操作人'" + antiCounterfeitingUpdateContract.getContacts() + "'失败，操作人手机号码已存在");
        }

        antiCounterfeitingUpdateContract.setApplicantId(userId);
        antiCounterfeitingUpdateContract.setApplicantName(loginUser.getUser().getNickName());
        antiCounterfeitingUpdateContract.setApplyTime(new Date());
        antiCounterfeitingUpdateContract.setStatus(1);
        antiCounterfeitingUpdateContractMapper.insertSelective(antiCounterfeitingUpdateContract);
        return AjaxResult.success(antiCounterfeitingUpdateContract.getApplicantId());
    }

    /**
     * 查询申请修改联系人列表
     *
     * @param antiCounterfeitingUpdateContract
     * @return
     */
    @Override
    public List<Map<String, Object>> list(AntiCounterfeitingUpdateContract antiCounterfeitingUpdateContract) {
        return antiCounterfeitingUpdateContractMapper.selectList(antiCounterfeitingUpdateContract);
    }


    /**
     * 处理修改联系人审批
     *
     * @param antiCounterfeitingUpdateContract
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void processApplications(AntiCounterfeitingUpdateContract antiCounterfeitingUpdateContract) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        antiCounterfeitingUpdateContract.setHandlerId(loginUser.getUser().getUserId());
        antiCounterfeitingUpdateContractMapper.processApplications(antiCounterfeitingUpdateContract);
        Integer status = antiCounterfeitingUpdateContract.getStatus();
        AntiCounterfeitingUpdateContract selectByPrimaryKey = antiCounterfeitingUpdateContractMapper.selectByPrimaryKey(antiCounterfeitingUpdateContract.getApplyId());
        String contacts = selectByPrimaryKey.getContacts();
        String contactNumber = selectByPrimaryKey.getContactNumber();
        String contactMobile = selectByPrimaryKey.getContactMobile();
        Long userId = selectByPrimaryKey.getApplicantId();
        if (status == 2) {
            sysUserMapper.updateContractsByUserName(userId,contacts,contactNumber,contactMobile);
            iSysNoticeService.stationLetterNotify(userId, "申请修改联系人审批结果", "申请成功", ProcessOptItemEnum.ANTI_COUNTERFEITING_UPDATE_CONTRACT.getItem(), antiCounterfeitingUpdateContract.getApplyId());
        }else{
            iSysNoticeService.stationLetterNotify(userId, "申请修改联系人审批结果", "申请失败", ProcessOptItemEnum.ANTI_COUNTERFEITING_UPDATE_CONTRACT.getItem(), antiCounterfeitingUpdateContract.getApplyId());
        }


    }

    /**
     * 导出申请修改联系人列表
     * @param updateContract
     * @param response
     * @throws IOException
     */
    @Override
    public void export(AntiCounterfeitingUpdateContract updateContract,HttpServletResponse response) throws IOException, InvalidFormatException {
        List<LinkedHashMap<String, Object>> list = antiCounterfeitingUpdateContractMapper.exportList(updateContract);
        ExcelTemplateExportUtils.baseExportWorkbook(list,response, ExpertFileNameEnum.anti_counterfeiting_update_contract.getFileName(), ExpertFilePathEnum.anti_counterfeiting_update_contract.getFilePath());
    }
}
